package ui.activity.poscontrol;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import net.ck.hzq.R;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.ScreenUtil;
import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import dialog.FlowRuleDialog;
import event.NetErrorEvent;
import ui.activity.poscontrol.beanmodel.PosMainBean;
import ui.activity.poscontrol.biz.PosControlMainBiz;
import ui.activity.poscontrol.component.DaggerPosControlMainComponent;
import ui.activity.poscontrol.contract.PosControlMainContract;
import ui.activity.poscontrol.module.PosControlMainModule;
import ui.activity.poscontrol.presenter.PosControlMainPresenter;
import util.CommonViewUtil;
import widget.DarkPopup;

import static com.qmuiteam.qmui.widget.popup.QMUIPopup.DIRECTION_BOTTOM;


/**
 * 收藏
 * Created by chenjian on 18/2/22.
 */

public class PosControlMainAct extends BaseAct implements PosControlMainContract.View {

    @Inject
    PosControlMainPresenter presenter;
    @Inject
    PosControlMainBiz biz;

    @BindView(R.id.needsx)
    TextView needsx;//右上角更多按钮

    @BindView(R.id.my_device_head)
    RelativeLayout my_device_head;
    @BindView(R.id.pushback_list)
    RelativeLayout pushback_list;//回拨记录

    @BindView(R.id.changeback_list)
    RelativeLayout changeback_list;//返现金额记录
    @BindView(R.id.flowed_device_head)
    RelativeLayout flowed_device_head;//已划拨终端列表
    @BindView(R.id.my_pos_num)
    TextView my_pos_num;
    @BindView(R.id.posrecord_txt_total)
    TextView posrecord_txt_total;
    @BindView(R.id.posopen_txt_total)
    TextView posopen_txt_total;
    @BindView(R.id.posrecord_txt_jh)
    TextView posrecord_txt_jh;
    @BindView(R.id.push_pos_num)
    TextView push_pos_num;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;



    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_poscontrolmain);
    }

    @Override
    protected void setup() {
        DaggerPosControlMainComponent.builder().posControlMainModule(new PosControlMainModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_poscontrol_main_title), "");
        presenter.setBiz(biz);

    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {

        CommonViewUtil.setDarw(needsx, this,-1 , -1,
                R.mipmap.icon_add_white, -1);
        RxView.clicks(needsx)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.showRightPopWindow());
        RxView.clicks(my_device_head)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toMyPosControl());
        RxView.clicks(pushback_list)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toPushBackControl());
        RxView.clicks(flowed_device_head)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toHasPushControl());
        RxView.clicks(changeback_list)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toChangeBackControl());
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.queryInfoData();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.queryInfoData();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }




    @Override
    public void upDateUI(PosMainBean bean) {
        my_pos_num.setText(bean.getPosCount()+"台");
        posrecord_txt_total.setText("终端数量："+(bean.getPosCount()+bean.getFlowCount())+"台");
        posopen_txt_total.setText("已开通："+bean.getOpenedCount()+"台");
        posrecord_txt_jh.setText("已激活"+bean.getActiveCount()+"台");
        push_pos_num.setText(bean.getFlowCount()+"台");
        ptr.onRefreshComplete();
    }

    @Override
    public void showPopWindow() {
        View inflate = LayoutInflater.from(this).inflate(R.layout.popup_device_flow, null);
        QMUIPopup qmuiPopup = new DarkPopup(this, DIRECTION_BOTTOM);
        qmuiPopup.setContentView(inflate);
        qmuiPopup.setPopupLeftRightMinMargin(ScreenUtil.dip2px(this, 5));
        qmuiPopup.show(needsx);
        inflate.findViewById(R.id.flow).setVisibility(View.GONE);
        inflate.findViewById(R.id.directionalallot).setVisibility(View.GONE);
        inflate.findViewById(R.id.rule).setOnClickListener(v -> {
            qmuiPopup.dismiss();
            presenter.queryRule();
        });
    }

    @Override
    public void showFlowRuleDialog(String items) {
        if (!TextUtils.isEmpty(items)) {
            FlowRuleDialog dialog = FlowRuleDialog.newInstance(items);
            dialog.show(getSupportFragmentManager(), "");
        }
    }
}
